* ======================================
* -- Win probability for politicians ---
* ======================================

	
	use $dta\PanelPoliticians.dta, clear


	gen bonus = Polit_Bonus
	gen party = Polit_Party
	gen rank = Polit_Rank
	replace rank = 40 if rank > 40 & rank < . 	// Merge spots above 40
	
	egen zz_loc = group(party year rank bonus)
	bysort zz_loc: gen sum_i = _N 				// Number of obs in each cell
		
		
	* Run regression model n times and create a prediction for obs. i, 
	* but set to missing obs. that are in cells "alone".
	* Split into parties to make code run faster.
	* Final iteration at 20000 since biggest party has about 20000 candidates.
	foreach p in rodt a frp h krf sp sv v mdg{

		preserve
		
		gen prWin_`p' = .
		keep if party == "`p'"

		gen id = _n
		forvalues i=1(1)20000{
			qui reg Polit_Win i.zz_loc if id!=`i', nocons
			qui predict prWin_`i'
			qui replace prWin_`p' = prWin_`i' ///
				if id == `i' & sum_i != 1
			drop prWin_`i'
		}
		
		keep Polit_id year prWin_`p'
		tempfile prWin_`p'
		save `prWin_`p''
		
		restore
	}

	* Import each party block and merge into single variable
	gen Polit_prWin = .
	foreach p in rodt a frp h krf sp sv v mdg{
		merge 1:1 Polit_id year using `prWin_`p'', keepusing(prWin_`p') nogen
		replace Polit_prWin = prWin_`p' if party == "`p'"
	}

	drop prWin_* bonus party rank zz_loc sum_i
	
	
	save $dta\PrWin.dta, replace